/**
 * DemoService.java
 * @author xsm
 * date 2012-8-8
 */

package com.netistate.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.netistate.dao.DemoDao;
import com.netistate.dao.ProductDao;
import com.netistate.pojo.Demo;
import com.netistate.pojo.Product;

@Service
@Transactional
public class DemoService {
	
	
	private DemoDao demoDao;
	private ProductDao productDao;

	/**
	 * 查询所有Demo
	 * @return
	 */
	public List<Demo> findAll() {
		return demoDao.findAll();
	}
	
	/**
	 * 查询全部demo
	 * @return
	 */
	public List<Product> findAllProduct() {
		return productDao.findAll();
	}	

	/**
	 * 保存Demo
	 * @param d
	 */
	public void save(Demo d) {
		demoDao.save(d);
	}

	/**
	 * 根据demoId去找demo
	 * @param did
	 * @return
	 */
	public Demo findDemoById(int did) {
		return demoDao.findById(did);
	}
	
	/**
	 * 更新demo
	 */
	public void update(Demo demo) {
		demoDao.save(demo);		
	}

	/**
	 * 删除Demo
	 * @param did
	 */
	public void del(int did) {
		demoDao.del(did);
		
	}
	
	/**
	 * 默认查询所有的demo,只显示最新的那条
	 * @param list 
	 * @return
	 */
	public List<Demo> findAllDemo(List<Integer> list) {
		
		List<Demo> dlist = new ArrayList<Demo>();
		for(int i : list){
			List<Demo> d= demoDao.findAllDemo(i);
			dlist.add(d.get(0));
		}
		return dlist;
	}
	
	
	/**
	 * 保存product
	 * @param p
	 */
	public void saveProduct(Product p) {
		productDao.save(p);
		
	}
	
	/**
	 * 删除product
	 * @param pid
	 */
	public void delProduct(int pid) {
		productDao.del(pid);		
	}
	
	/**
	 * 根据Productid找product
	 * @param pid
	 * @return
	 */
	public Product findProductById(int pid) {
		return productDao.findById(pid);
	}
	
	/**
	 * 更新产品
	 * @param pro
	 */
	public void updateProduct(Product pro) {
		productDao.save(pro);
	}
	/**
	 * 根据orders去排序
	 * @return
	 */
	public List<Integer> findAllDemoByOrders() {
		return demoDao.findAllDemoByOrders();
	}

	/**
	 * 根据orders,查询某个Demo的全部信息
	 * @param orders
	 * @return
	 */
	public List<Demo> findOneDemoByOrders(String orders) {
		int o = Integer.parseInt(orders);
		return demoDao.findOneDemoByOrdes(o);
	}

	/**
	 *根据productNo去找Product 
	 * @param productNo
	 * @return
	 */
	public List<Product> findProductByProductNo(String productNo) {
		List<Product> pList = new ArrayList<Product>();
		if("所有".equals(productNo)){
			pList = productDao.findAll();
		}else{
			pList = productDao.findProductByProductNo(productNo);
		}
		return pList; 
	}

	/**
	 * 点击添加项目服务表时，选择POC,勾选显示相关的demo信息时，就拿pid去找demo
	 * @param pid
	 * @return
	 */
	public Demo findDemoByPid(String pid) {
		
		int id = Integer.parseInt(pid);
		
		List<Demo> demoList = demoDao.findDemoByPid(id);
		Demo d = demoList.get(0);
		return d;
	}

	@Autowired
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	@Autowired
	public void setDemoDao(DemoDao demoDao) {
		this.demoDao = demoDao;
	}

}
